package com.teng.smartdrawer;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.teng.smartdrawer.utils.ImageLoadingConfig;
import com.teng.smartdrawer.utils.PreferenceUtils;
import com.teng.smartdrawer.view.CircleImageView;


public class LogOutActivity extends AppCompatActivity implements View.OnClickListener {

    private PreferenceUtils preferenceUtils;

    private ImageLoader imageLoader = ImageLoader.getInstance();
    private DisplayImageOptions imgOptions = ImageLoadingConfig.generateDisplayImageOptionsWithAnimationAndNoCorner(R.mipmap.user_default);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_out);

        ActionBar actionBar = getSupportActionBar();

        //显示返回图表
        actionBar.setDisplayHomeAsUpEnabled(true);

        //设置ActionBar左边默认的图标是否可用
        actionBar.setDisplayUseLogoEnabled(true);

        // 显示窗口标题
        actionBar.setDisplayShowTitleEnabled(true);

        actionBar.setTitle("我");

        preferenceUtils = PreferenceUtils.getInstance(this);

        TextView user_account = (TextView) findViewById(R.id.user_account);

        user_account.setText(preferenceUtils.getString(PreferenceUtils.USER_ACCOUNT_NUMBER, "admin"));

        CircleImageView user_pic = (CircleImageView) findViewById(R.id.user_pic);

        String pic_url = preferenceUtils.getString(PreferenceUtils.USER_HEAD_PIC , "http://img2.pconline.com.cn/pconline/0706/19/1038447_34.jpg");


        imageLoader.displayImage(pic_url, user_pic, imgOptions);

        TextView tv_logout = (TextView) findViewById(R.id.tv_logout);
        tv_logout.setOnClickListener(this);

        TextView count = (TextView) findViewById(R.id.count);
        count.setOnClickListener(this);

        TextView change_account = (TextView) findViewById(R.id.change_account);
        change_account.setOnClickListener(this);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(item.getItemId() == android.R.id.home)
        {
            // TODO actionBar返回处理
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_logout:

                new AlertDialog.Builder(this)
                        .setTitle("退出")
                        .setMessage("确定要退出吗")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                MyApplication.isLogin = false;
                                preferenceUtils.addBoolean(PreferenceUtils.USER_IS_LOGIN, false);
                                setResult(RESULT_OK);
                                finish();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();

                break;

            case R.id.count:

                Intent intent = new Intent(LogOutActivity.this , HistoryActivity.class);
                startActivity(intent);

                break;

            case R.id.actionbar_back:
                finish();
                break;

            case R.id.change_account:
                Intent loginIntent = new Intent(LogOutActivity.this , LoginActivity.class);
                preferenceUtils.addBoolean(PreferenceUtils.USER_IS_LOGIN , false);
                startActivity(loginIntent);
                finish();
                break;

            default:

                break;
        }
    }
}
